﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once
#include "ElementId.h"
#include "IGroup.h"
#include "GbmpModel.h"

namespace gcmp
{
    class IDocument;


    class GBMP_MODEL_EXPORT GroupManager
    {
    public:
        static GroupManager& GetInstance(IDocument* pDoc);
        ~GroupManager();

    public:
        // 指定组内构件为同步状态
        void SetSync(const gcmp::ElementId& groupId, const gcmp::ElementId& elementId);
        void SetSync(const gcmp::ElementId& groupId, const std::vector<gcmp::ElementId>& elementIds);

        // 指定组内构件为不同步状态
        void SetUnsync(const gcmp::ElementId& groupId, const gcmp::ElementId& elementId);
        void SetUnsync(const gcmp::ElementId& groupId, const std::vector<gcmp::ElementId>& elementIds);

        // 获取组内构件的同步/不同步状态
        bool IsSync(const gcmp::ElementId& groupId, const gcmp::ElementId& elementId) const;
        bool IsUnsync(const gcmp::ElementId& groupId, const gcmp::ElementId& elementId) const;

    private:
        GroupManager();

        /*!
        *@brief      查找某元素在组类型中的同位元素，此元素是组内第一层嵌套组实例内的元素
        *@author     linn-e
        *@date       2021年07月29日
        *@param[in]  pDoc 文档指针
        *@param[in]  pParentGroup groupId表示的组所属的组
        *@param[in]  groupId 源元素所在的组
        *@param[in]  elementId 源元素
        *@return 源元素在组类型中同位元素的ElementId
        */
        ElementId GetRelativeElementIdInGroupType(IDocument* pDoc, const IGroup* pParentGroup, const ElementId& groupId, const ElementId& elementId) const;

        IGroupType* GetGroupType(const IGroup* pGroup) const;

        void AddReferenceElement(const IGroup* pParentGroup, const ElementId& groupId, const ElementId& elementIdInGroup) const;
        void RemoveReferenceElement(const IGroup* pParentGroup, const ElementId& groupId, const ElementId& elementIdInGroup) const;

    private:
        // 所有组中被设置为不同步状态的构件记录
        // key : group id
        // value : unsync element ids in group
        std::map<gcmp::ElementId, std::set<gcmp::ElementId>> m_unsyncGroupElementRecord;
        gcmp::IDocument* m_pDocument;
    };
}
